﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Xml;

    public class CmsCatchHack
    {
        private readonly KeRui.DAL.CmsCatchParse dal = new KeRui.DAL.CmsCatchParse();
        private static RegexOptions regex = (RegexOptions.Multiline | RegexOptions.IgnoreCase);

        public static bool CheckHack(string content)
        {
            return ((content.Length > 7) && (content.ToLower().Substring(0, 7) == "hack://"));
        }

        public static string Get163Flv(string liveurl)
        {
            Exception exception;
            string xml = "";
            WebClient client = new WebClient();
            client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
            try
            {
                xml = client.DownloadString(liveurl);
            }
            catch (Exception exception1)
            {
                exception = exception1;
            }
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(xml);
                return document.SelectNodes("all/flvUrl/flv")[0].InnerText;
            }
            catch (Exception exception2)
            {
                exception = exception2;
                return Utils.SplitString(xml, ";")[0];
            }
        }

        public static string[] GetContentImage(string str, int type)
        {
            string strContent = "";
            MatchCollection matchs = Regex.Matches(str, "<img([\\s\\S]*?)src=\"(?<img>[\\s\\S]*?)\"([\\s\\S]*?)>", regex);
            foreach (Match match in matchs)
            {
                string str3 = match.Groups["img"].Value.Trim();
                if (type == 0)
                {
                    strContent = strContent + "|" + str3;
                }
                else if (type == 1)
                {
                    if ((str3.Length > 8) && (str3.ToLower().Substring(0, 8) == "/upload/"))
                    {
                        strContent = strContent + "|" + str3;
                    }
                }
                else if ((type == 2) && ((str3.Length > 7) && (str3.ToLower().Substring(0, 7) == "http://")))
                {
                    strContent = strContent + "|" + str3;
                }
            }
            if (strContent != "")
            {
                strContent = strContent.Substring(1);
            }
            return Utils.SplitString(strContent, "|");
        }

        public static string GetContentSummary(string title, string summary, string content)
        {
            title = title.Trim();
            content = content.Trim();
            summary = Utils.RemoveHtml(summary);
            if (summary != "")
            {
                return summary;
            }
            if (content != "")
            {
                MatchCollection matchs = Regex.Matches(content, @"<p>(?<content>[\s\S]*?)</p>", regex);
                string str = "";
                foreach (Match match in matchs)
                {
                    GroupCollection groups = match.Groups;
                    if (Utils.RemoveHtml(groups["content"].Value.Trim()) != "")
                    {
                        str = Utils.RemoveHtml(groups["content"].Value.Trim());
                        break;
                    }
                }
                if (str != "")
                {
                    return str;
                }
                str = Utils.RemoveHtml(content);
                if (str != "")
                {
                    return str;
                }
            }
            return title;
        }

        public static string GetFlvHtml(string id, string src, int width, int height)
        {
            string str3 = "";
            str3 = ((((str3 + "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" id=\"" + id + "\" width=\"" + width.ToString() + "\" height=\"" + height.ToString() + "\" codebase=\"http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab\">") + "<param name=\"movie\" value=\"/aspx/swf/flvplayer.swf\" />") + "<param name=\"quality\" value=\"high\" />" + "<param name=\"menu\" value=\"false\" />") + "<param name=\"allowFullScreen\" value=\"true\" />" + "<param name=\"allowScriptAccess\" value=\"always\" />") + "<param name=\"FlashVars\" value=\"vcastr_file=" + src + "\" />";
            return ((str3 + "<embed name=\"" + id + "\" src=\"/aspx/swf/flvplayer.swf\" FlashVars=\"vcastr_file=" + src + "\" quality=\"high\" menu=\"false\" width=\"" + width.ToString() + "\" height=\"" + height.ToString() + "\" play=\"true\" loop=\"false\" allowScriptAccess=\"always\" allowfullscreen=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.adobe.com/go/getflashplayer\" />") + "</object>");
        }

        public static string GetHackContent(string content)
        {
            if (!CheckHack(content))
            {
                return "";
            }
            string str = "hack://" + GetHackName(content) + "://";
            return content.Substring(str.Length, content.Length - str.Length);
        }

        public static string GetHackName(string content)
        {
            if (!CheckHack(content))
            {
                return "";
            }
            return Utils.SplitString(content, "://")[1].ToLower();
        }

        public static string GetHtml(string url)
        {
            WebClient client;
            url = url.Replace("&amp;", "&");
            if (url.Length < 7)
            {
                return "";
            }
            if (url.ToLower().Substring(0, 7) != "http://")
            {
                return "";
            }
            string str = "";
            try
            {
                client = new WebClient();
                client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                str = client.DownloadString(url);
                client.Dispose();
            }
            catch (Exception)
            {
                try
                {
                    client = new WebClient();
                    client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                    str = client.DownloadString(url);
                    client.Dispose();
                }
                catch (Exception)
                {
                    return "";
                }
            }
            return HttpUtility.HtmlDecode(str.Replace("&nbsp;", " "));
        }

        public static string GetHtml(string url, string code)
        {
            WebClient client;
            byte[] buffer;
            if (code == "")
            {
                return GetHtml(url);
            }
            url = url.Replace("&amp;", "&");
            if (url.Length < 7)
            {
                return "";
            }
            if (url.ToLower().Substring(0, 7) != "http://")
            {
                return "";
            }
            string str = "";
            try
            {
                client = new WebClient();
                client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                buffer = client.DownloadData(url);
                str = Encoding.GetEncoding(code).GetString(buffer);
                client.Dispose();
            }
            catch (Exception)
            {
                try
                {
                    client = new WebClient();
                    client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                    buffer = client.DownloadData(url);
                    str = Encoding.GetEncoding(code).GetString(buffer);
                    client.Dispose();
                }
                catch (Exception)
                {
                    return "";
                }
            }
            return HttpUtility.HtmlDecode(str.Replace("&nbsp;", " "));
        }

        public static string GetSwfHtml(string id, string src, int width, int height)
        {
            string str3 = "";
            str3 = (((str3 + "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" id=\"" + id + "\" width=\"" + width.ToString() + "\" height=\"" + height.ToString() + "\" codebase=\"http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab\">") + "<param name=\"movie\" value=\"" + src + "\" />") + "<param name=\"quality\" value=\"high\" />" + "<param name=\"menu\" value=\"false\" />") + "<param name=\"allowFullScreen\" value=\"true\" />" + "<param name=\"allowScriptAccess\" value=\"always\" />";
            return ((str3 + "<embed name=\"" + id + "\" src=\"/aspx/swf/flvplayer.swf\" quality=\"high\" menu=\"false\" width=\"" + width.ToString() + "\" height=\"" + height.ToString() + "\" play=\"true\" loop=\"false\" allowScriptAccess=\"always\" allowfullscreen=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.adobe.com/go/getflashplayer\" />") + "</object>");
        }

        public static string Hack163Flv(string swfstr)
        {
            string src = Hack163FlvUrl(swfstr);
            if (src == "")
            {
                return GetSwfHtml("CoreVPlayer", swfstr, 0x1e2, 0x183);
            }
            return GetFlvHtml("FlvPlayer", src, 0x1e2, 0x183);
        }

        public static string Hack163FlvPic(string swfstr)
        {
            string[] strArray = Utils.SplitString(swfstr, "~");
            return ("http://" + strArray[3] + ".jpg");
        }

        public static string Hack163FlvUrl(string swfstr)
        {
            string[] strArray = Utils.SplitString(swfstr, "~");
            return Get163Flv("http://live.ws.126.net/video/" + strArray[2].Substring(strArray[2].Length - 2, 1) + "/" + strArray[2].Substring(strArray[2].Length - 1, 1) + "/" + strArray[2] + ".xml");
        }

        public static string HackUrl(string curUrl, string tarUrl)
        {
            string str = tarUrl;
            if (tarUrl.Length < 7)
            {
                return "";
            }
            if (tarUrl.Substring(0, 7) == "http://")
            {
                return str;
            }
            string[] strArray = Utils.SplitString(curUrl, "/");
            if (tarUrl.Substring(0, 1) == "/")
            {
                return ("http://" + strArray[2] + tarUrl);
            }
            return (curUrl.Substring(0, curUrl.Length - strArray[strArray.Length - 1].Length) + tarUrl);
        }

        public static string SavePic(string url, string img, KeRui.Model.CmsServer ServerInfo)
        {
            int num;
            Exception exception;
            img = HackUrl(url, img);
            string str = "";
            string path = img;
            path = path.Replace("http://", "");
            string str3 = "";
            string[] strArray = Utils.SplitString(ServerInfo.ServerDir, @"\\");
            str3 = strArray[0];
            for (num = 1; num < strArray.Length; num++)
            {
                str3 = str3 + @"\" + strArray[num];
                if (!Directory.Exists(str3))
                {
                    Directory.CreateDirectory(str3);
                }
            }
            string[] strArray2 = Utils.SplitString("Upload/Catch/" + path, "/");
            for (num = 0; num < (strArray2.Length - 1); num++)
            {
                str = str + "/" + strArray2[num];
                str3 = str3 + @"\" + strArray2[num];
                try
                {
                    if (!Directory.Exists(str3))
                    {
                        Directory.CreateDirectory(str3);
                    }
                }
                catch (Exception exception1)
                {
                    exception = exception1;
                    Utils.ErrorNote("图片", "失败，无法获得图片。img：" + img + "，详情：" + exception.Message);
                    return img;
                }
            }
            str = str + "/" + strArray2[strArray2.Length - 1];
            path = str3 + @"\" + strArray2[strArray2.Length - 1];
            if (!System.IO.File.Exists(path))
            {
                WebClient client;
                try
                {
                    client = new WebClient();
                    client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                    client.DownloadFile(img, path);
                    client.Dispose();
                }
                catch (Exception)
                {
                    try
                    {
                        client = new WebClient();
                        client.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
                        client.DownloadFile(img, path);
                        client.Dispose();
                    }
                    catch (Exception exception4)
                    {
                        exception = exception4;
                        Utils.ErrorNote("图片", "失败，无法获得图片。img：" + img + "，详情：" + exception.Message);
                        return img;
                    }
                }
            }
            return str;
        }
    }
}

